package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.bean.BaseBean;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RegistActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.btn_regist_next)
    Button btnRegistNext;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.et_regist_phone)
    EditText etRegistPhone;
    @Bind(R.id.et_regist_code)
    EditText etRegistCode;
    @Bind(R.id.btn_regist_getcode)
    Button btnRegistGetcode;
    private String code="";
    CountDownTimer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        ButterKnife.bind(this);
        initView();
        initData();
        initEvent();
    }


    @Override
    public void onEventMainThread(MainEvent event) {
        super.onEventMainThread(event);
        if("registSuccese".equals(event.msg))
        {
            finish();
        }
    }
    public void initData() {
//        protol.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        timer = new CountDownTimer(60000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
//                btnRegistGetcode.setTextColor(getResources().getColor(R.color.cl_red));
                btnRegistGetcode.setText((millisUntilFinished / 1000) + "s");
            }

            @Override
            public void onFinish() {
                btnRegistGetcode.setClickable(true);
//                btnRegistGetcode.setTextColor(getResources().getColor(R.color.cl_white));
                btnRegistGetcode.setText("发送");
            }
        };

    }

    @Override
    protected void onPause() {
        super.onPause();
        timer.cancel();
        btnRegistGetcode.setClickable(true);
        btnRegistGetcode.setText("发送");
    }
    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_regist, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    private void initEvent() {
        btnRegistGetcode.setOnClickListener(this);
        btnRegistNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_regist_next:
                if(Utils.isEmpty(etRegistPhone.getText().toString())){
                    T.showShort(this,R.string.str_phone_notnull);
                }else if(11!=etRegistPhone.getText().length())
                {
                    T.showShort(this,R.string.str_phone_false);
                }else if(Utils.isEmpty(etRegistCode.getText().toString())){
                    T.showShort(this,getString(R.string.str_code_notnull));
                }else if(!etRegistCode.getText().toString().equals(code)){
                    T.showShort(this,getString(R.string.str_code_false));
                }else{
                    Intent intent=new Intent(this, RegistPsdActivity.class);
                    intent.putExtra("phone",etRegistPhone.getText().toString());
                    intent.putExtra("code",etRegistCode.getText().toString());
                    startActivity(intent);
                }

                break;
            case R.id.btn_regist_getcode:
                if(Utils.isEmpty(etRegistPhone.getText().toString())){
                    T.showShort(this,R.string.str_phone_notnull);
                }else if(11!=etRegistPhone.getText().length())
                {
                    T.showShort(this,R.string.str_phone_false);
                }else{

                    getCode();
                }
                break;
        }

    }

    private void getCode() {
        String timeline=Utils.getTimeline();
        String sign=Utils.md5(timeline);
        getTestApi().getCode(timeline,sign,etRegistPhone.getText().toString())
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BaseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        T.showLong(RegistActivity.this,R.string.net_error);
                    }

                    @Override
                    public void onNext(BaseBean baseBean) {
                        L.d(baseBean.getMsg());
                        if (Utils.checkResultCode(baseBean.getReturn_code()))
                        {
                            btnRegistGetcode.setClickable(false);
                            timer.start();
                            code=baseBean.getMsg();
                            T.showShort(RegistActivity.this,"验证码已发送至手机");
                        }else{
                            T.showLong(RegistActivity.this,baseBean.getMsg());
                        }
                    }
                });
    }
}
